//
//  Author.m
//  FuncFanc
//
//  Created by hubing on 2018/4/16.
//  Copyright © 2018年 huhu. All rights reserved.
//

#import "Author.h"
@interface Author ()

@end

@implementation Author

-(instancetype)initWithName:(NSString *)name{
    if (self = [super init]) {
        [self tangPoets];
        [self songPoets];
        [self lyricPoets];
        _name = name;
    }
    return self;
}

-(NSString *)tangPoetDesc{
    NSString *detail = @"";
    for (NSDictionary *dict  in _tangPoets) {
        if ([dict[@"name"]isEqualToString:_name]) {
            detail = dict[@"desc"];
            }
        }
        return detail;
}

-(NSString *)songPoetDesc{
     NSString *detail = @"";
    for (NSDictionary *dict in _songPoets) {
        if ([dict[@"name"] isEqualToString:_name]) {
            detail = dict[@"desc"];
        }
    }
    return detail;
}


-(NSArray *)tangPoets{
    NSString *tangPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingPathComponent:@"authors.tang.json"];
    NSURL *tangUrl = [NSURL fileURLWithPath:tangPath];
    NSData *tangData = [[NSData alloc]initWithContentsOfURL:tangUrl];
    _tangPoets = [NSJSONSerialization JSONObjectWithData:tangData options:0 error:NULL];
    return _tangPoets;
}

-(NSArray *)songPoets{
    NSString *songPath = [[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:@"authors.song.json"];
    NSURL *url = [NSURL fileURLWithPath:songPath];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    _songPoets = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
    return _songPoets;
}

-(NSArray *)lyricPoets{
    NSString *songPath = [[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:@"author.song.json"];
    NSURL *url = [NSURL fileURLWithPath:songPath];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    _lyricPoets = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
    return _lyricPoets;
}



@end

//#include <libkern/OSAtomic.h>
//#include <execinfo.h>
//
//
//
//NSString *const UncaughtExceptionHandlerSignalExceptionName =@"UncaughtExceptionHandlerSignalExceptionName";
//
//NSString *const UncaughtExceptionHandlerSignalKey =@"UncaughtExceptionHandlerSignalKey";
//
//NSString *const UncaughtExceptionHandlerAddressesKey =@"UncaughtExceptionHandlerAddressesKey";
//
//volatile int32_t UncaughtExceptionCount = 0;
//
//const int32_t UncaughtExceptionMaximum =10;
//
//const NSInteger UncaughtExceptionHandlerSkipAddressCount =4;
//
//const NSInteger UncaughtExceptionHandlerReportAddressCount =5;
//
//@implementation UncaughtExceptionHandler
//
//-(NSArray *)backtrace {
//    void callstack[128];
//    int frames =backtrace(callstack, 128);
//    char **strs =backtrace_symbols(callstack, frames);
//    int i;
//    NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
//    for (i = UncaughtExceptionHandlerSkipAddressCount ; i <UncaughtExceptionHandlerSkipAddressCount +UncaughtExceptionHandlerReportAddressCount; i++){
//    [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];
//    }
//    free(strs);
//    return backtrace;
//}
//
//-(void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex {
//    if (anIndex ==0){
//        _dismissed =YES;
//    }else if (anIndex==1) {
//        NSLog(@"ssssssss");
//    }
//}
//
//-(void)handleException:(NSException *)exception {
//    [self validateAndSaveCriticalApplicationData];
//
//    CFRunLoopRef runLoop = CFRunLoopGetCurrent();
//    CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);
//    while (!_dismissed) {
//        for (NSString *mode in (__bridge NSArray *)allModes) {
//            CFRunLoopRunInMode((CFStringRef)mode,0.001, false);
//        }
//    }
//
//    CFRelease(allModes);
//    NSSetUncaughtExceptionHandler(NULL);
//    signal(SIGABRT,SIG_DFL);
//    signal(SIGILL,SIG_DFL);
//    signal(SIGSEGV,SIG_DFL);
//    signal(SIGFPE,SIG_DFL);
//    signal(SIGBUS,SIG_DFL);
//    signal(SIGPIPE,SIG_DFL);
//
//    [exception raise];
//    if ([[exception name] isEqual:UncaughtExceptionHandlerSignalExceptionName]) {
//        kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKey]intValue]);
//    }else{
//        [exception raise];
//    }
//}
//
//@end
//
//void HandleException(NSException *exception) {
//    int32_t exceptionCount =OSAtomicIncrement32(&UncaughtExceptionCount);
//    if (exceptionCount >UncaughtExceptionMaximum) {
//        return;
//    }
//    NSArray *callStack = [UncaughtExceptionHandler backtrace];
//    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];[userInfo setObject:callStack forKey:UncaughtExceptionHandlerAddressesKey];
//
//    [[[UncaughtExceptionHandler alloc] init]performSelectorOnMainThread:@selector(handleException:)withObject:
//    [NSException exceptionWithName:[exception name] reason:[exception reason] userInfo:userInfo]waitUntilDone:YES];
//    
//}
//void SignalHandler(int signal) {
//    int32_t exceptionCount =OSAtomicIncrement32(&UncaughtExceptionCount);
//    if (exceptionCount >UncaughtExceptionMaximum) {
//    return;
//    }
//    NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:UncaughtExceptionHandlerSignalKey];
//
//    NSArray *callStack = [UncaughtExceptionHandler backtrace];
//    [userInfo setObject:callStack forKey:UncaughtExceptionHandlerAddressesKey];
//
//    [[[UncaughtExceptionHandler alloc] init] performSelectorOnMainThread:@selector(handleException:)withObject:[NSException exceptionWithName:UncaughtExceptionHandlerSignalExceptionName reason:[NSString stringWithFormat:NSLocalizedString(@"Signal %d was raised.",nil),signal]userInfo:
//                                                                                                              [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:signal]forKey:UncaughtExceptionHandlerSignalKey]]waitUntilDone:YES];
//}
//
//
//void InstallUncaughtExceptionHandler(void) {
//
//    NSSetUncaughtExceptionHandler(&HandleException);
//
//    signal(SIGHUP, SignalHandler);
//    signal(SIGINT, SignalHandler);
//    signal(SIGQUIT, SignalHandler);
//
//
//    signal(SIGABRT,SignalHandler);
//    signal(SIGILL,SignalHandler);
//    signal(SIGSEGV,SignalHandler);
//    signal(SIGFPE,SignalHandler);
//    signal(SIGBUS,SignalHandler);
//    signal(SIGPIPE,SignalHandler);
//
//}
